package com.my.es.test.doc;

import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/5/9 16:54
 */
public class ESTest_Doc_Query {

    public static void main(String[] args) throws IOException {
        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
        
        // 1.查询索引中全部的数据
        /*SearchRequest request = new SearchRequest();
        request.indices("user");
        
        
        request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));

        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        System.out.println("hits.getTotalHits() = " + hits.getTotalHits());
        System.out.println("hits.getHits() = " + hits.getHits());
        System.out.println("response.getTook() = " + response.getTook());
        for (SearchHit hit : hits) {
            System.out.println("hit.getSourceAsString() = " + hit.getSourceAsString());
        }*/

        // 条件查询：termQuery
        /*SearchRequest request = new SearchRequest();
        request.indices("user");


        request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age", 30)));

        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        System.out.println("hits.getTotalHits() = " + hits.getTotalHits());
        System.out.println("hits.getHits() = " + hits.getHits());
        System.out.println("response.getTook() = " + response.getTook());
        for (SearchHit hit : hits) {
            System.out.println("hit.getSourceAsString() = " + hit.getSourceAsString());
        }*/
        
        // 3.分页查询
        /*SearchRequest request = new SearchRequest();
        request.indices("user");

        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());

        // (当前页码 - 1) * 每页显示条数
        builder.from(2);
        builder.size(2);
        request.source(builder);

        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        System.out.println("hits.getTotalHits() = " + hits.getTotalHits());
        System.out.println("hits.getHits() = " + hits.getHits());
        System.out.println("response.getTook() = " + response.getTook());
        for (SearchHit hit : hits) {
            System.out.println("hit.getSourceAsString() = " + hit.getSourceAsString());
        }*/

        // 4.查询排序
        /*SearchRequest request = new SearchRequest();
        request.indices("user");

        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.termQuery("age", 30));
        // 降序
        builder.sort("age", SortOrder.DESC);

        request.source(builder);

        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        System.out.println("hits.getTotalHits() = " + hits.getTotalHits());
        System.out.println("hits.getHits() = " + hits.getHits());
        System.out.println("response.getTook() = " + response.getTook());
        for (SearchHit hit : hits) {
            System.out.println("hit.getSourceAsString() = " + hit.getSourceAsString());
        }*/

        // 过滤字段
        /*SearchRequest request = new SearchRequest();
        request.indices("user");

        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        String[] excludes = {};
        String[] includes = {"name"};
        builder.fetchSource(includes, excludes);
        request.source(builder);

        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        System.out.println("hits.getTotalHits() = " + hits.getTotalHits());
        System.out.println("hits.getHits() = " + hits.getHits());
        System.out.println("response.getTook() = " + response.getTook());
        for (SearchHit hit : hits) {
            System.out.println("hit.getSourceAsString() = " + hit.getSourceAsString());
        }*/
        
        // 6. 组合查询
        /*SearchRequest request = new SearchRequest();
        request.indices("user");

        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        builder.query(boolQueryBuilder);
        
        //boolQueryBuilder.must(QueryBuilders.matchQuery("age", 30));
        //boolQueryBuilder.must(QueryBuilders.matchQuery("sex", "男"));
        //boolQueryBuilder.mustNot(QueryBuilders.matchQuery("sex", "男"));
        boolQueryBuilder.should(QueryBuilders.matchQuery("age", 30));
        boolQueryBuilder.should(QueryBuilders.matchQuery("age", 40));
        request.source(builder);

        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        System.out.println("hits.getTotalHits() = " + hits.getTotalHits());
        System.out.println("hits.getHits() = " + hits.getHits());
        System.out.println("response.getTook() = " + response.getTook());
        for (SearchHit hit : hits) {
            System.out.println("hit.getSourceAsString() = " + hit.getSourceAsString());
        }*/
        
        // 7. 范围查询
        /*SearchRequest request = new SearchRequest();
        request.indices("user");

        SearchSourceBuilder builder = new SearchSourceBuilder();
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
        rangeQuery.gte(30);
        rangeQuery.lte(50);
        builder.query(rangeQuery);

        request.source(builder);

        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        System.out.println("hits.getTotalHits() = " + hits.getTotalHits());
        System.out.println("hits.getHits() = " + hits.getHits());
        System.out.println("response.getTook() = " + response.getTook());
        for (SearchHit hit : hits) {
            System.out.println("hit.getSourceAsString() = " + hit.getSourceAsString());
        }*/
        
        // 8.模糊查询
        /*SearchRequest request = new SearchRequest();
        request.indices("user");

        SearchSourceBuilder builder = new SearchSourceBuilder();
        // 差一个字符的可以查询出来，fuzziness 偏差值
        FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name", "wangwu").fuzziness(Fuzziness.ONE);
        builder.query(fuzzyQueryBuilder);
        request.source(builder);

        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        System.out.println("hits.getTotalHits() = " + hits.getTotalHits());
        System.out.println("hits.getHits() = " + hits.getHits());
        System.out.println("response.getTook() = " + response.getTook());
        for (SearchHit hit : hits) {
            System.out.println("hit.getSourceAsString() = " + hit.getSourceAsString());
        }
        
        
        esClient.close();*/
        
        // 9. 高亮查询
        /*SearchRequest request = new SearchRequest();
        request.indices("user");

        SearchSourceBuilder builder = new SearchSourceBuilder();
        TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("name", "zhangsan");
        builder.query(termsQueryBuilder);

        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        highlightBuilder.field("name");
        
        builder.highlighter(highlightBuilder);
        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
        System.out.println("response.toString() = " + response.toString());

        SearchHits hits = response.getHits();
        System.out.println("hits.getTotalHits() = " + hits.getTotalHits());
        System.out.println("hits.getHits() = " + hits.getHits());
        System.out.println("response.getTook() = " + response.getTook());
        for (SearchHit hit : hits) {
            System.out.println("hit.getSourceAsString() = " + hit.getSourceAsString());
        }*/
        
        // 10.聚合查询
        /*SearchRequest request = new SearchRequest();
        request.indices("user");

        SearchSourceBuilder builder = new SearchSourceBuilder();
        MaxAggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
        builder.aggregation(aggregationBuilder);
        
        
        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
        System.out.println("response.toString() = " + response.toString());

        SearchHits hits = response.getHits();
        System.out.println("hits.getTotalHits() = " + hits.getTotalHits());
        System.out.println("hits.getHits() = " + hits.getHits());
        System.out.println("response.getTook() = " + response.getTook());
        for (SearchHit hit : hits) {
            System.out.println("hit.getSourceAsString() = " + hit.getSourceAsString());
        }*/
        
        // 11.分组查询
        SearchRequest request = new SearchRequest();
        request.indices("user");

        SearchSourceBuilder builder = new SearchSourceBuilder();
        TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
        builder.aggregation(aggregationBuilder);


        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
        System.out.println("response.toString() = " + response.toString());

        SearchHits hits = response.getHits();
        System.out.println("hits.getTotalHits() = " + hits.getTotalHits());
        System.out.println("hits.getHits() = " + hits.getHits());
        System.out.println("response.getTook() = " + response.getTook());
        for (SearchHit hit : hits) {
            System.out.println("hit.getSourceAsString() = " + hit.getSourceAsString());
        }


        esClient.close();
    }
    
}
